minetest.register_abm({
	nodenames = {
		"group:crumbly", "group:snappy", "group:cracky", "group:choppy", "group:melts"
		--, "flowers:rose"
	},
	--neighbors = {"default:air"},
	interval = 30.0, 
	chance = 900, --1 a cada X (Padrao: = 600)
	action = function (pos, node, active_object_count, active_object_count_wider)
		if pos.y < 0 then return end
		local nodeAcima = lunoweather.addvectors(pos, {x=0, y=1, z=0})
		local nodeAbaixo = lunoweather.addvectors(pos, {x=0, y=-1, z=0})


		if lunoschedule.getMineMount() ==4 and minetest.setting_getbool("lunoweather_snowmap") then  --Se diferente do Inverno
			--local snow = lunoweather.get_snow(pos)
			--if snow == 0 then return end
		
			--[[
			if minetest.registered_nodes[node.name].drawtype ~= "normal"
				and minetest.registered_nodes[node.name].drawtype ~= "nodebox"
				and minetest.registered_nodes[node.name].drawtype ~= "allfaces_optional" 
			then 
				print("minetest.registered_nodes["..node.name.."].drawtype = "..minetest.registered_nodes[node.name].drawtype)
				return 
			end
			]]--
			local sand = lunoweather.nodes_in_range(pos, 8, "default:sand")
			local desert_sand = lunoweather.nodes_in_range(pos, 8, "default:desert_sand")
			local torch = lunoweather.nodes_in_range(pos, 3, "default:torch")
			local lava_flowing = lunoweather.nodes_in_range(pos, 4, "default:lava_flowing")
			local lava_source = lunoweather.nodes_in_range(pos, 4, "default:lava_source")

		
			if --Se o usuario fizer uma construcao no deserto, nao sera congelada.
				sand >= 4 
				or desert_sand >= 4 
				or torch >= 1
				or lava_flowing >= 1 
				or lava_source >= 1  
				--or minetest.registered_nodes[node.name].drawtype == "torchlike"
				or minetest.registered_nodes[node.name].drawtype == "signlike"
				or node.name == "default:sign_wall"
				or node.name == "default:sand"
				or node.name == "default:desert_sand"
				or node.name == "default:desert_stone"
				or node.name == "lunocartas:mailbox"
				or node.name == "lunodecor:doorbell"
				or node.name == "lunotrades:balcaodeloja"
				or node.name == "default:sing_wall"
				or node.name == "default:papyrus"
				or node.name == "default:fence_wood"
				or node.name == "stairs:stair_wood"
				or node.name == "stairs:stair_cobble"
				or node.name == "doors:door_steel_t_1"
				or node.name == "doors:door_wood_t_1"
				or node.name == "doors:door_glass_t_1"
				or node.name == "doors:trapdoor"
				or node.name == "craft_guide:ldc_pc"
			then --Tipo de blocos nao afetados
				return 
			else
				--print("minetest.registered_nodes["..node.name.."].drawtype = "..minetest.registered_nodes[node.name].drawtype)
			end
			
			if minetest.env:get_node_light(nodeAcima, 0.5) ~= 15 then return end --para nao congela a casa!
		
			local addsnow = 1
		
			if minetest.env:get_node(pos).name == "default:snow" then
				if minetest.env:add_node_level(pos, 4) > 0 then
					minetest.env:add_node(pos, {name="default:ice"})
				else
					addsnow = 0
				end
			end
		
			if addsnow > 0 and minetest.env:get_node(nodeAcima).name == "air" then
				if  minetest.registered_nodes[node.name].drawtype == "plantlike" then --Tipo de dano dependendo se ser um planta ou outro bloco.
					if minetest.env:get_node(nodeAbaixo).name ~= "default:sand" then --Se a planta for sobre a areia a neve nao atinge!
						--print("############### Congelando planta => "..node.name.." em "..minetest.pos_to_string(pos))
						minetest.env:add_node(pos, {name="default:snow"})
						minetest.env:add_node_level(pos)
					end
				else
					minetest.env:add_node(nodeAcima, {name="default:snow"})
					minetest.env:add_node_level(nodeAcima)
				end
			end
		else --se diferente do inverno
			if minetest.env:get_node(pos).name == "default:snow" then
				--print("############### Descongelando a neve em "..minetest.pos_to_string(pos))
				minetest.remove_node(pos)
				if minetest.env:get_node(nodeAbaixo).name == "default:dirt_with_snow" then
					minetest.env:add_node(nodeAbaixo, {name="default:dirt_with_grass"})
				end
			elseif minetest.env:get_node(pos).name == "default:dirt_with_snow" then
				--print("############### Descongelando a terra em "..minetest.pos_to_string(pos))
				if minetest.env:get_node(nodeAcima).name == "air" or minetest.env:get_node(nodeAcima).name == "default:snow" then
					minetest.env:add_node(pos, {name="default:dirt_with_grass"})
				else
					minetest.env:add_node(pos, {name="default:dirt"})
				end
				if minetest.env:get_node(nodeAcima).name == "default:snow" then
					minetest.remove_node(nodeAcima)
				end
			end
		end
	end
})

--[[
local snow_box =
{
	type  = "fixed",
	fixed = {-0.5, -0.5, -0.5, 0.5, -0.4, 0.5}
}
]]--


--[[
-- Enable this section if you have a very fast PC
minetest.register_abm({
	nodenames = {"group:crumbly", "group:snappy", "group:cracky", "group:choppy", "group:melts"},
	neighbors = {"default:air"},
	interval = 10.0, 
	chance = 80,
	--interval = 1.0, 
	--chance = 5,
	action = function (pos, node, active_object_count, active_object_count_wider)
		local snow = lunoweather.get_snow(pos)
		if snow == 0 then return end
		
		
		if minetest.registered_nodes[node.name].drawtype ~= "normal"
			and minetest.registered_nodes[node.name].drawtype ~= "nodebox"
			and minetest.registered_nodes[node.name].drawtype ~= "allfaces_optional" then return end
		local np = lunoweather.addvectors(pos, {x=0, y=1, z=0})
		if minetest.env:get_node_light(np, 0.5) ~= 15 then return end
		local addsnow = 1
		if minetest.env:get_node(pos).name == "default:snow" then
			if minetest.env:add_node_level(pos, 4) > 0 then
				minetest.env:add_node(pos, {name="default:ice"})
			else
				addsnow = 0
			end
		end
		if addsnow > 0 and minetest.env:get_node(np).name == "air" then
			minetest.env:add_node(np, {name="snow"})
			minetest.env:add_node_level(np)
		end
	end
})
--]]
